<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('system.rabbitmq.group_name') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table />

        <!-- 表单 -->
        <PopupForm />
        <Messagebox />
    </div>
</template>

<script setup lang="ts">
import { concat, cloneDeep } from 'lodash-es'
import { provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useI18n } from 'vue-i18n'

import Messagebox from './messagebox.vue'

defineOptions({
    name: '/system/rabbitmq',
})

const { t } = useI18n()
const adminInfo = useAdminInfo()
let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: 'info',
        title: 'Info',
        text: t('system.rabbitmq.audit'),
        type: 'primary',
        icon: '',
        class: 'table-row-info',
        disabledTip: false,
        click: (row: TableRow) => {
            infoButtonClick(row)
        },
    },
]
// const optButtons = defaultOptButtons(['edit', 'delete', 'messagebox'])
optButtons = concat(defaultOptButtons(['edit', 'delete']), optButtons)
optButtons[1].display = (row) => {
    return row.id != adminInfo.id
}

const baTable = new baTableClass(
    new baTableApi('/systems/systemsMq/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: false, width: 70 },
            { label: t('system.rabbitmq.group_name'), prop: 'group_name', align: 'center', operator: false },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                operator: false,
                render: 'tag',
                custom: { '2': 'danger', '1': 'success' },
                replaceValue: { '2': t('Disable'), '1': t('Enable') },
            },
            { label: t('system.rabbitmq.mq_config'), prop: 'mq_config', align: 'center', operator: false },
            { label: t('system.rabbitmq.queue_name'), prop: 'queue_name', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('system.rabbitmq.exchange_name'), prop: 'exchange_name', align: 'center', operator: false },
            { label: t('system.rabbitmq.bind_key'), prop: 'bind_key', align: 'center', operator: false },
            {
                label: t('system.rabbitmq.queue_function'),
                prop: 'queue_function',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            { label: t('system.rabbitmq.max_process_num'), prop: 'max_process_num', align: 'center', operator: false },
            { label: t('system.rabbitmq.max_do_ready_num'), prop: 'max_do_ready_num', align: 'center', operator: false },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: ['all']
    },
    {
        defaultItems: {
            status: '1',
        },
    }
)

provide('baTable', baTable)
/** 点击查看详情按钮响应 */
const infoButtonClick = (row: TableRow) => {
    if (!row) return
    // 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = {
        id: row.id,
        status: row.status,
    }
    baTable.form.loading = true
    baTable.form.items = rowClone
    baTable.form.operate = 'audit'
    baTable.form.loading = false
}
baTable.mount()
baTable.getIndex()
</script>

<style scoped lang="scss"></style>
